/**
 * @file
 * @auth jinguangguo
 * @date 2017/01/19
 */

import Vue from 'vue';
import _ from 'lodash';
import filter from '../../../../../grsc-base/util/filter';
import mathtool from '../../../../../grsc-base/util/mathtool';
import parseUrl from '../../../../../grsc-base/util/parseUrl';
import Loading from '../../../../../grsc-base/component/Loading.vue';
import Pager from '../../../../../grsc-base/component/Pager.vue';
import NoData from '../../../../../grsc-base/component/NoData1.vue';
import {PAGE_SIZE} from '../../../../../grsc-base/common/constants.js';
import lib from '../../../../common/lib';
import tpl from './tpl.html';
import service from '../service';
import util from '../util';
import CONFIG from '../config';

export default Vue.extend({
    template: tpl,
    components: {
        Pager,
        NoData,
        Loading
    },
    data() {
        let coin = this.$$symbol.split('_')[1];

        return {
            lib,
            list: null,
            isLoading: true,
            filter: filter,
            CONFIG: CONFIG,
            pageNo: 1,
            pageNum: 0,
            coin: coin
        };
    },
    computed: {

    },
    created() {
        this.fetchList();
    },
    methods: {

        _initDetailById(list) {
            let query = parseUrl();
            if (query.id) {
                let id = query.id;
                let item = _.find(list, {
                    id: +id
                });
                this.toggleDetail(item);
            }
        },

        /**
         * 获取数据列表
         */
        fetchList() {
            this.isLoading = true;

            //获取当前时间
            function getNowFormatDate() {
                var day = new Date();
                var Year = 0;
                var Month = 0;
                var Day = 0;
                var Hou=0;
                var min=0;
                var s=0;
                var CurrentDate = "";
                //初始化时间
                //Year       = day.getYear();//有火狐下2008年显示108的bug
                Year       = day.getFullYear();//ie火狐下都可以
                Month      = day.getMonth()+1;
                Day        = day.getDate();
                Hou=day.getHours();
                min=day.getMinutes()
                s=day.getSeconds()

                CurrentDate += Year + "-";
                if (Month >= 10 ) {
                    CurrentDate += Month + "-";
                } else {
                    CurrentDate += "0" + Month + "-";
                }
                if (Day >= 10 ) {
                    CurrentDate += Day ;
                } else{
                    CurrentDate += "0" + Day ;
                }

                // if(Hou>=10){
                //     CurrentDate += "+"+Hou + ":";
                // }else{
                //     CurrentDate += "0" + Hou + ":";
                // }
                //
                // if(min>=10){
                //     CurrentDate += min + ":";
                // }else{
                //     CurrentDate += "0" + min + ":";
                // }
                //
                // if(s>=10){
                //     CurrentDate +=  s ;
                // }else{
                //     CurrentDate += "0" + s ;
                // }



                return CurrentDate;
            }

            //获取七天前时间
            function getBeforeDate(n){
                var n = n;
                var d = new Date();
                var sum= ' ';
                var year = d.getFullYear();
                var mon=d.getMonth()+1;
                var day=d.getDate();
                var Hou=d.getHours();
                var min=d.getMinutes();
                var s=d.getSeconds();
                if(day <= n){
                    if(mon>1) {
                        mon=mon-1;
                    }
                    else {
                        year = year-1;
                        mon = 12;
                    }
                }

                if(s<10){
                    s='0'+s
                }

                if(min<10){
                    min='0'+min
                }

                d.setDate(d.getDate()-n);
                year = d.getFullYear();
                mon=d.getMonth()+1;
                day=d.getDate();
                sum = year+"-"+(mon<10?('0'+mon):mon)+"-"+(day<10?('0'+day):day);//+"+"+Hou+":"+min+":"+s;
                return sum;
            }

            let lastDate = getNowFormatDate();
            let startDate = getBeforeDate(7);

            service.tradeCoinHistoryList({
                symbol: this.$$symbol,
                pageNo: this.pageNo,
                pageSize: 50,    // 默认50条全部显示
                beginTime:startDate,
                endTime:lastDate,
            }).done((rep) => {
                this.isLoading = false;
                if (rep.code == window.CODE.SUCCESS) {
                    this.list = this._getFormattedList(rep.data.list);
                    this.pageNum = rep.data.pageNum;
                    if (this.list.length > 0) {
                        this._initDetailById(this.list);
                    }
                } else {
                    window.error(rep.msg);
                }
            }).fail((rep) => {

            });
        },

        changePage(pageNo) {
            this.pageNo = pageNo;
            this.fetchList()
        },

        /**
         * 打开/关闭详情
         * @param item
         */
        /*
        toggleDetail(item) {
            if (!item.isOpening) {
                // 标识已经打开
                item.isOpening = true;
                item.isLoading = true;
                service.tradeDetail({
                    type: item.tradeCoinType,
                    transactionNo: item.transactionNo,
                    pageNo: 1,
                    pageSize: PAGE_SIZE
                }).done((rep) => {
                    item.isLoading = false;
                    if (rep.code === window.CODE.SUCCESS) {
                        let detail = rep.data;
                        setDetailTotalNum(detail.list);
                        // 记录当前记录的页码
                        detail.pageNo = 1;
                        item.detail = detail;
                    } else {
                        window.error(rep.msg);
                    }
                });
            } else {
                // 标识已经关闭
                item.isOpening = false;
                item.detail = null;
            }
        },
        */

        /**
         * 查看更多
         * @param item
         */
        loadMore(item) {
            const pageNo = item.detail.pageNo + 1;
            service.tradeDetail({
                type: item.tradeCoinType,
                transactionNo: item.transactionNo,
                pageNo: pageNo,
                pageSize: PAGE_SIZE
            }).done((rep) => {
                if (rep.code === window.CODE.SUCCESS) {
                    let detailMore = rep.data.list;
                    setDetailTotalNum(detailMore);
                    // 连接list
                    item.detail.list = item.detail.list.concat(detailMore);
                    // 记录当前记录的页码
                    item.detail.pageNo = pageNo;
                } else {
                    window.error(rep.msg);
                }
            });
        },

        _getFormattedList(list) {
            let that = this;

            $.map(list, function (item, index) {
                // // 日期
                // item.createDateHtml = util.getDateHtml(item.createTime);
                // 类型
                item.tradeCoinTypeHtml = util.getTradeTypeHtml(item.tradeCoinType);

                // 平均价
                setAveragePrice(item);

                // 初始化交易详情
                item.detail = null;
            });
            return list;

            /**
             * 设置平均价
             * @param item
             */
            function setAveragePrice(item) {
                item.tradedCoin = mathtool.reduce(item.number, item.numberOver);

                if (item.tradedCoin <= 0) {
                    item.averagePrice = 0;
                } else {
                    item.averagePrice = item.matchedMoney / item.tradedCoin;
                }
            }

        }
    }
});

// 设置详情总价格
function setDetailTotalNum(list) {
    list.map((item, index) => {
        item.totalNum = mathtool.multiplication(item.price, item.num, 4);
    });
}
